#include "login.h"
#include "ui_login.h"

#include <QDebug>
#include <QPushButton>
#include <QMessageBox>
#include <QKeyEvent>
#include <QFile>
#include <QString>

QString username, password, code;
QStringList list, name;
QTcpSocket *s;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_DeleteOnClose);

    QFile file(":/login.qss"); // 假设style.qss位于资源文件中
    if (file.open(QFile::ReadOnly))
    {
        setStyleSheet(QString(file.readAll())); // 应用全局样式
        file.close();
    }

    setWindowFlag(Qt::FramelessWindowHint,true);//去掉主窗口的边框
    setAttribute(Qt::WA_TranslucentBackground,true);//把主窗口透明

    s = new QTcpSocket(this);

    qDebug()<<"客户端初始化完毕";

    QFile listFile(":/city.txt");
    if (listFile.open(QFile::ReadOnly))
    {
        QTextStream read(&listFile);
        // read.setCodec("UTF-8");
        while(false == read.atEnd())
        {
            QString line = read.readLine();
            ::list << line;
            ::name << QStringList(line.split('\t'))[1];
//            qDebug()<<"list:"<<list.last()<<"name:"<<name.last();
        }
        listFile.close();
    }

    // r = new regist();
    // r->hide();
}

MainWindow::~MainWindow()
{
    qDebug()<<"MainWindow";
    delete s;
    delete ui;
    delete r;
    // delete m;
}

void MainWindow::disconnectedSlot()
{
}

void MainWindow::readyReadSlot()
{
    QString msg = QString::fromLocal8Bit(s->readAll());
    qDebug()<<"l:"<<msg;
    if("password error" == msg)
    {
        QToolTip::showText(ui->passwordLineEdit->mapToGlobal(QPoint(25, 15)), "密码输入错误，请重新输入密码！");
    }
    else if("user no exist" == msg)
    {
        QToolTip::showText(ui->usernameLineEdit->mapToGlobal(QPoint(25, 15)), "用户不存在，请重新输入用户名！");
    }
    else
    {
        QStringList list = msg.split(' ');
        for(int i = 0; i < ::list.size(); i++)
        {
            QStringList city = ::list[i].split('\t');
            if(list[2] == city[1])
            {
                code = city[0];
                break;
            }
        }
        username = ui->usernameLineEdit->text();
        password = ui->passwordLineEdit->text();
        mainInterface * m = new mainInterface(this);
        m->show();
        this->hide();
    }
    s->disconnectFromHost();

}

void MainWindow::connectedSlot()
{
    QString msg = QString("login") + ' ';
    msg += ui->usernameLineEdit->text() + ' ' + ui->passwordLineEdit->text();
    s->write(msg.toLocal8Bit());
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(false == ui->loginButton->underMouse() && false == ui->registButton->underMouse() &&
       false == ui->closeButton->underMouse() && false == ui->label->underMouse())
        mouseOff = event->globalPos() - geometry().topLeft();
    ui->passwordLineEdit->clearFocus();
    ui->usernameLineEdit->clearFocus();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(false == ui->loginButton->underMouse() && false == ui->registButton->underMouse() &&
        false == ui->closeButton->underMouse() && false == ui->label->underMouse())
        this->move(event->globalPos() - mouseOff);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(Qt::Key_Enter == event->key() || Qt::Key_Return == event->key())
        on_loginButton_clicked();
}

void MainWindow::on_loginButton_clicked()
{
    if("" == ui->usernameLineEdit->text())
    {
        QToolTip::showText(ui->usernameLineEdit->mapToGlobal(QPoint(25, 15)), "账号为空，请输入账号！");
    }
    else if("" == ui->passwordLineEdit->text())
    {
        QToolTip::showText(ui->passwordLineEdit->mapToGlobal(QPoint(25, 15)), "密码为空，请输入密码！");
    }
    else
    {
        connect(s, &QTcpSocket::connected, this, &MainWindow::connectedSlot);
        connect(s, &QTcpSocket::readyRead, this, &MainWindow::readyReadSlot);
        connect(s, &QTcpSocket::disconnected, this, &MainWindow::disconnectedSlot);

        s->connectToHost("192.168.32.188",9000);
        qDebug()<<"login";

    }
}

void MainWindow::on_closeButton_clicked()
{
    // QApplication* app;
    // app->exit(0);
    this->close();
}

void MainWindow::on_registButton_clicked()
{
    regist * r = new regist(this);
    r->show();
}

